<template>
    <div>
        <van-nav-bar title="登录" left-arrow @click-left="() => { $router.push('/home/index') }" fixed />
        <div style="height: 45px;"></div>
        <van-form @submit="onSubmit">
            <van-cell-group inset>
                <van-field v-model="username" name="username" left-icon="graphic" placeholder="请输入手机号"
                    :rules="[{ pattern, message: '请输入手机号' }]" />
                         <van-field v-model="ncname" name="ncname" left-icon="graphic" placeholder="昵称"
                    :rules="[{ required: true, message: '请填写昵称' }]" />
                <van-field v-model="sms" center clearable left-icon="shield-o" placeholder="请输入验证码"
                    :rules="[{ required: true, message: '请输入短信验证码' }]">
                    <template #button>
                        <van-button size="small" type="primary" @click="yzm"
                            :disabled="time ? true : false">{{ time ? time : '获取验证码' }}</van-button>
                    </template>
                </van-field>
                <van-field v-model="password" :type="start ? 'password' : 'text'" name="password" left-icon="lock"
                    placeholder="6位及以上字母数字组合" :rules="[{ pattern: passworda, message: '6位及以上字母数字组合' }]">
                    <template #button>
                        <van-icon name="eye-o" @click="start = !start" />
                    </template>
                </van-field>
            </van-cell-group>
            <div style="margin: 16px;">
                <van-button block type="primary" native-type="submit" style="background-color: #eee;border: none;">
                    登录
                </van-button>
            </div>
        </van-form>
        <div style="margin: 16px;">
            <van-button round block type="primary" native-type="submit" @click="zc">
                注册
            </van-button>
        </div>
    </div>
</template>

<script setup>
import { ref } from 'vue';
const username = ref('');
const ncname = ref('');
const password = ref('');
const pattern = /^1[1-9]\d{9}$/
const passworda = /^\d{6}/
const sms = ref('')
const time = ref('')
const start = ref(true)
let timer;
const yzm = () => {
    time.value = 10
    sms.value = Math.floor(Math.random() * 1000000)
    timer = setInterval(() => {
        if (time.value == 0) return
        time.value--
        if (time.value == 0) {
            clearInterval(timer)
        }
    }, 1000);
}
import { useRouter } from 'vue-router';
const router = useRouter()
import { showSuccessToast, showFailToast } from 'vant';
import { loginApi } from '@/api/api';
const onSubmit = (values) => {
    console.log('submit', values);
    loginApi(username.value, password.value).then(res => {
        console.log(res);
        if (res.data.code == 0) {
            showSuccessToast('登录成功');
            console.log(res.data);
            
            localStorage.setItem('token', res.data.data.token)
            localStorage.setItem('username', res.data.data.username)
            localStorage.setItem('ncname',values.ncname)
            localStorage.setItem('ncname',values.ncname)
            router.push('/home/index')
        } else {
            showFailToast('登录失败');
        }
    })
};
const zc=()=>{
       router.push('/register')
}
</script>

<style lang="scss" scoped></style>